from selenium import webdriver
from lxml import etree
import time

from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()


def get_info(url, page):
    page = page + 1
    driver.get(url)
    driver.implicitly_wait(10)
    selector = etree.HTML(driver.page_source)
    infos = selector.xpath('//*[@id="J_goodsList"]/ul')
    for info in infos:
        # 价格
        price = info.xpath('li/div/div[3]/strong/i/text()')
        print(price)
        # 店铺
        shop = info.xpath('li/div/div[7]/span/a/text()')
        print(shop)
    if page <= 5:
        NextPage(url, page)
    else:
        pass


def NextPage(url, page):  # 下一页
    driver.get(url)
    driver.implicitly_wait(10)
    # driver.find_element_by_xpath('//*[@id="J_bottomPage"]').click()
    driver.find_element(By.XPATH, '//*[@id="J_bottomPage"]').click()
    time.sleep(10)
    driver.get(driver.current_url)
    driver.implicitly_wait(10)
    get_info(driver.current_url, page)


if __name__ == '__main__':
    page = 1
    url = 'https://www.jd.com/'
    driver.get(url)
    driver.implicitly_wait(10)
    # driver.find_element_by_id('key').clear()
    # driver.find_element_by_id('key').send_keys('手机')
    # driver.find_element_by_class_name('button').click()
    driver.find_element(By.ID, 'key').clear()
    driver.find_element(By.ID, 'key').send_keys('手机')
    driver.find_element(By.CLASS_NAME, 'button').click()
    time.sleep(3)
    get_info(driver.current_url, page)
